package org.xx.armory.swing.builders;

import org.xx.armory.objectparser.ParsedObjectBuilder;

public class CellModelBuilder
        extends ParsedObjectBuilder<CellModel>
        implements ContainerModelBuilder {
    private ComponentModel child;
    private int rowSpan;
    private int columnSpan;
    private ModelAlignment horizontalAlignment;
    private ModelAlignment verticalAlignment;
    private int margin;


    public CellModelBuilder() {
    }

    public final CellModelBuilder setRowSpan(
            int rowSpan
    ) {
        this.rowSpan = rowSpan;
        return this;
    }

    public final CellModelBuilder setColumnSpan(
            int columnSpan
    ) {
        this.columnSpan = columnSpan;
        return this;
    }

    public final CellModelBuilder setHorizontalAlignment(
            ModelAlignment horizontalAlignment
    ) {
        this.horizontalAlignment = horizontalAlignment;
        return this;
    }

    public final CellModelBuilder setVerticalAlignment(
            ModelAlignment verticalAlignment
    ) {
        this.verticalAlignment = verticalAlignment;
        return this;
    }

    public final CellModelBuilder setMargin(
            int margin
    ) {
        this.margin = margin;
        return this;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public CellModelBuilder addChild(
            ComponentModel child
    ) {
        if (this.child != null) {
            throw new IllegalStateException("child of cell is not null: " + this.child);
        }

        this.child = child;
        return this;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public CellModel build() {
        return new CellModel(getLineInfo(),
                             -1, // 座标尚未确定。
                             -1, // 座标尚未确定。
                             this.rowSpan,
                             this.columnSpan,
                             this.horizontalAlignment,
                             this.verticalAlignment,
                             -1, // 横向自动拉伸比例尚未确定。
                             -1, // 纵向自动拉伸比例尚未确定。
                             this.margin,
                             this.child);
    }
}
